package com.example.lovedogs.fragment.zizhen;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;


import java.util.TooManyListenersException;

import com.example.lovedogs.R;
import com.example.lovedogs.domain.SickInfoBody;
import com.example.lovedogs.domain.SickInfoState;
import com.example.lovedogs.domain.parent.SickInfo;
import com.example.lovedogs.util.SpanUtil;
import com.example.lovedogs.zizhen.SickInfoDiseaseActivity;
import com.example.lovedogs.zizhen.SickInfoDiseaseDetailsActivity;
import com.example.lovedogs.zizhen.SickInfoStateActivity;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.graphics.Color;
import android.os.Bundle;
import android.text.SpannableStringBuilder;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class SickInfoStateFragmentLeft extends Fragment{
	private SickInfoStateFragmentRight rightFragment;
	private LayoutInflater inflater;
	private View view;
	private StateAdapter adapter;
	private BodyAdapter bodyAdapter;
	private ListView  lv_sickinfostate;
	private SickInfoStateActivity currentactivity;
//	private String url="http://10.40.7.187:8080/LoveDogs04/JsonSickInfoStateServlet";
	private String url;
//	FragmentRight rightFragment;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		this.inflater=inflater;
		currentactivity= (SickInfoStateActivity) getActivity();
		//info得到查询的是根据state or body  
		String info=currentactivity.getIntent().getStringExtra("info");
//		System.out.println(currentactivity.getIntent().getStringExtra("info"));
		if(view == null){
			view  = inflater.inflate(R.layout.fragment_sickinfostate_left, container, false);
			lv_sickinfostate=(ListView) view.findViewById(R.id.lv_sickinfostate);
			
			if("state".equals(info)){
				//适配sickinfo_state、sickinfo_sdetails表中拿来的数据
				url=currentactivity.getString(R.string.serverurl)+"JsonSickInfoStateServlet";
				lvAdapterStateData();
				lv_sickinfostate.setOnItemClickListener(new StateOnItemClickListener());
			}
			if("body".equals(info)){
				url=currentactivity.getString(R.string.serverurl)+"JsonSickInfoBodyServlet";
				lvAdapterBodyData();
				lv_sickinfostate.setOnItemClickListener(new BodyOnItemClickListener());
			}
			
		}
		return view;
	}

	private void lvAdapterBodyData() {
		
		HttpUtils http=new HttpUtils();
		http.send(
				HttpMethod.POST, 
				url, 
				new RequestCallBack<String>() {

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						Gson g=new Gson();
						Type type=new TypeToken<List<SickInfoBody>>(){}.getType();
						List<SickInfoBody> data=g.fromJson(responseInfo.result, type);
						int firstClassno=data.get(0).getNo();
						System.out.println(firstClassno+"*************http*************");
						if (rightFragment==null) {
							rightFragment = currentactivity.getFragmentRight();
						}
						rightFragment.updateBdetails(firstClassno);
						
						
						if (bodyAdapter==null) {
							bodyAdapter = new BodyAdapter(data);
							
						}else{
							bodyAdapter.setData(data);
						}
						bodyAdapter.notifyDataSetChanged();
						lv_sickinfostate.setAdapter(bodyAdapter);
						
					}

					@Override
					public void onFailure(HttpException error, String msg) {
						Toast.makeText(currentactivity,
								"onErrorResponse" + error.getMessage(),
								Toast.LENGTH_LONG).show();
						
					}
				});
		
		
	}

	private void lvAdapterStateData() {
		
		HttpUtils http=new HttpUtils();
		http.send(
				HttpMethod.POST, 
				url, 
				new RequestCallBack<String>() {

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						Gson g=new Gson();
						Type type=new TypeToken<List<SickInfoState>>(){}.getType();
						List<SickInfoState> data=g.fromJson(responseInfo.result, type);
						int firstClassno=data.get(0).getNo();
						System.out.println(firstClassno+"*************http*************");
						if (rightFragment==null) {
							rightFragment = currentactivity.getFragmentRight();
						}
						rightFragment.updateSdetails(firstClassno);
						
						
						if (adapter==null) {
							adapter = new StateAdapter(data);
							
						}else{
							adapter.setData(data);
						}
						adapter.notifyDataSetChanged();
						lv_sickinfostate.setAdapter(adapter);
						
					}

					@Override
					public void onFailure(HttpException error, String msg) {
						Toast.makeText(currentactivity,
								"onErrorResponse" + error.getMessage(),
								Toast.LENGTH_LONG).show();
						
					}
				});
		
		
	}
	
	private class StateAdapter extends BaseAdapter{
		private List<SickInfoState> data;
		

		public StateAdapter(List<SickInfoState> data) {
			this.data=data;
		}
		
		public List<SickInfoState> getData() {
			return data;
		}

		public void setData(List<SickInfoState> data) {
			this.data = data;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return data.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return data.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			SickInfoState info=data.get(position);
			if (convertView==null) {
				convertView=inflater.inflate(R.layout.textview_item, null);
			}
			TextView tv=(TextView) convertView.findViewById(R.id.tv_item);
			tv.setText(info.getName());
			tv.setPadding(20, 20, 20, 20);
			return convertView;
		}
		
	}
	private class BodyAdapter extends BaseAdapter{
		private List<SickInfoBody> data;
		
		
		public BodyAdapter(List<SickInfoBody> data2) {
			this.data=data2;
		}
		
		public List<SickInfoBody> getData() {
			return data;
		}
		
		public void setData(List<SickInfoBody> data) {
			this.data = data;
		}
		
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return data.size();
		}
		
		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return data.get(position);
		}
		
		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			SickInfoBody info=data.get(position);
			if (convertView==null) {
				convertView=inflater.inflate(R.layout.textview_item, null);
			}
			TextView tv=(TextView) convertView.findViewById(R.id.tv_item);
			tv.setText(info.getName());
			tv.setPadding(20, 20, 20, 20);
			return convertView;
		}
		
	}
	
	private class StateOnItemClickListener implements OnItemClickListener{
		
		
		
		private int stateNo=0;
		private SickInfoState demo;
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			demo=(SickInfoState) adapter.getItem(position);
			stateNo=demo.getNo();
			if (rightFragment==null) {
				rightFragment = currentactivity.getFragmentRight();
			}
			System.out.println(stateNo+"**********************");
			rightFragment.updateSdetails(stateNo);
		}
		
	}
	private class BodyOnItemClickListener implements OnItemClickListener{
		
		
		
		private int stateNo=0;
		private SickInfoBody demo;
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			demo=(SickInfoBody) bodyAdapter.getItem(position);
			stateNo=demo.getNo();
			if (rightFragment==null) {
				rightFragment = currentactivity.getFragmentRight();
			}
			System.out.println(stateNo+"**********************");
			rightFragment.updateBdetails(stateNo);
		}
		
	}
}
